/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.datatables.QuestTable;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.quest.L1QuestTemp;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1SystemMessage;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.collections.Maps;

public class L1Quest {
	private static Logger _log = Logger.getLogger(L1Quest.class.getName());

	public static final int QUEST_LEVEL15 = 1;

	public static final int QUEST_LEVEL30 = 2;

	public static final int QUEST_LEVEL45 = 3;

	public static final int QUEST_LEVEL50 = 4;

	public static final int QUEST_LYRA = 10; // 擊退妖魔的契約

	public static final int QUEST_OILSKINMANT = 11; // 拿油布斗篷

	public static final int QUEST_DOROMOND = 20;

	public static final int QUEST_RUBA = 21;

	public static final int QUEST_AREX = 22;

	public static final int QUEST_LUKEIN1 = 23;

	public static final int QUEST_TBOX1 = 24;

	public static final int QUEST_TBOX2 = 25;

	public static final int QUEST_TBOX3 = 26;

	public static final int QUEST_SIMIZZ = 27;

	public static final int QUEST_DOIL = 28;

	public static final int QUEST_RUDIAN = 29;

	public static final int QUEST_RESTA = 30;

	public static final int QUEST_CADMUS = 31;

	public static final int QUEST_KAMYLA = 32;

	public static final int QUEST_CRYSTAL = 33;

	public static final int QUEST_LIZARD = 34; // 蜥蜴人的寶物

	public static final int QUEST_KEPLISHA = 35; // 占星術師凱莉紗

	public static final int QUEST_DESIRE = 36;

	public static final int QUEST_SHADOWS = 37;

	public static final int QUEST_ROI = 38;

	public static final int QUEST_TOSCROLL = 39; // 說話卷軸

	public static final int QUEST_MOONOFLONGBOW = 40; // 妖精族傳說中的弓

	public static final int QUEST_GENERALHAMELOFRESENTMENT = 41;

	public static final int QUEST_TUTOR = 300; //新手導師
	public static final int QUEST_TUTOR2 = 304; //修練場管理員

	public static final int QUEST_END = 255; // 終了済みクエストのステップ

	private L1PcInstance _owner = null;

	private Map<Integer, Integer> _quest = null;

	public L1Quest(L1PcInstance owner) {
		_owner = owner;
	}

	public L1PcInstance get_owner() {
		return _owner;
	}

	public int get_step(int quest_id) {
		if (_quest == null) {
			Connection con = null;
			PreparedStatement pstm = null;
			ResultSet rs = null;
			try {
				_quest = Maps.newMap();

				con = L1DatabaseFactory.getInstance().getConnection();
				pstm = con.prepareStatement("SELECT * FROM character_quests WHERE char_id=?");
				pstm.setInt(1, _owner.getId());
				rs = pstm.executeQuery();

				while (rs.next()) {
					_quest.put(new Integer(rs.getInt(2)), new Integer(rs.getInt(3)));
				}

			} catch (SQLException e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			} finally {
				SQLUtil.close(rs);
				SQLUtil.close(pstm);
				SQLUtil.close(con);
			}
		}
		
		Integer step = _quest.get(new Integer(quest_id));
		if (step == null) {
			return 0;
		} else {
			return step.intValue();
		}
	}

	public void set_step(int quest_id, int step) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			if (_quest.get(new Integer(quest_id)) == null) {

				pstm = con.prepareStatement("INSERT INTO character_quests " + "SET char_id = ?, quest_id = ?, quest_step = ?");
				pstm.setInt(1, _owner.getId());
				pstm.setInt(2, quest_id);
				pstm.setInt(3, step);
				pstm.execute();
			} else {
				pstm = con.prepareStatement("UPDATE character_quests " + "SET quest_step = ? WHERE char_id = ? AND quest_id = ?");
				pstm.setInt(1, step);
				pstm.setInt(2, _owner.getId());
				pstm.setInt(3, quest_id);
				pstm.execute();
			}
			if (step == 1) {
				_owner.sendPackets(new S_SystemMessage(L1SystemMessage.ShowMessage(1) + QuestTable.getInstance().getTemplate(quest_id).getName() + "。"));
			} else if (step == QUEST_END) {
				_owner.sendPackets(new S_SystemMessage(L1SystemMessage.ShowMessage(3) + QuestTable.getInstance().getTemplate(quest_id).getName() + "。"));
			} else {
				_owner.sendPackets(new S_SystemMessage(L1SystemMessage.ShowMessage(3) + QuestTable.getInstance().getTemplate(quest_id, step).getName() + "。"));
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
		_quest.put(new Integer(quest_id), new Integer(step));
	}
	/**
	 * 加入任務
	 * @param id
	 */
	// 		15,	30,	45, 50(S), 50(A)
	private String[][] _questName = {
			{"王族的自知"},			{"艾莉亞的請求"},		{"王族的信念"},			{"小惡魔的可疑行動"},		{"不死魔族再生的秘密"}, // 0-P
			{"瑞奇的抵抗"},			{"拯救被幽禁的吉姆"},		{"騎士的證明"},			{"精靈們的騷動"},		{"不死魔族再生的秘密"}, // 1-K
			{"詹姆的請求"},			{"不死族的叛徒"},		{"法師的考驗"},			{"取回間諜的報告書"},		{"不死魔族再生的秘密"}, // 2-W
			{"歐斯的先見之明"},		{"達克馬勒的威脅"},		{"妖精的任務"},			{"協助間諜大逃亡"},		{"不死魔族再生的秘密"}, // 3-E
			{"妖魔的侵入"},			{"同族的背叛"},			{"糾正錯誤的觀念"},		{"尋找黑暗之星"},		{""}, // 4-D
			{"行跡可疑的妖魔們"},		{"妖魔密使首領的情報"},	{"與幻術士締結同盟"},		{"時空彼端的線索(龍騎士)"},{""}, // 5-R
			{"眠龍洞穴污染的來源"},	{"艾爾摩戰場的軌跡"},		{"白蟻出現的理由"},		{"時空彼端的線索(幻術士)"},{""}, // 6-I
			};
	
	public void add_quest(int quest_id) {
		int step = get_step(quest_id);
		L1QuestTemp temp = QuestTable.getInstance().getTemplate(quest_id);
		if (quest_id <= 4) {
			if (_owner.isCrown()) {
				temp.setName(_questName[0][quest_id - 1]);
			} else if (_owner.isKnight()) {
				temp.setName(_questName[1][quest_id - 1]);
			} else if (_owner.isWizard()) {
				temp.setName(_questName[2][quest_id - 1]);
			} else if (_owner.isElf()) {
				temp.setName(_questName[3][quest_id - 1]);
			} else if (_owner.isDarkelf()) {
				temp.setName(_questName[4][quest_id - 1]);
			} else if (_owner.isDragonKnight()) {
				temp.setName(_questName[5][quest_id - 1]);
			} else if (_owner.isIllusionist()) {
				temp.setName(_questName[6][quest_id - 1]);
			}
		}
		
		if (step == QUEST_END) {
			if (temp.isRepeat()) {
				set_step(quest_id, 1);
			} else {
				_owner.sendPackets(new S_SystemMessage(L1SystemMessage.ShowMessage(3) + QuestTable.getInstance().getTemplate(quest_id).getName() + "。"));
				return;
			}
		} else if (step == 0) {
			set_step(quest_id, 1);
		}
	}
	/**
	 * 刪除任務
	 * @param id
	 */
	public void del_quest(int id) {
		if (get_step(id) == 0) {
			return;
		}
		
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			
			pstm = con.prepareStatement("DELETE FROM character_quests WHERE char_id = ? AND quest_id = ?");
			pstm.setInt(1, _owner.getId());
			pstm.setInt(2, id);
			pstm.execute();
			_owner.sendPackets(new S_SystemMessage(L1SystemMessage.ShowMessage(4) + QuestTable.getInstance().getTemplate(id).getName() + "。"));
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		_quest.remove(new Integer(id));
	}

	public void set_end(int quest_id) {
		set_step(quest_id, QUEST_END);
	}

	public boolean isEnd(int quest_id) {
		if (get_step(quest_id) == QUEST_END) {
			return true;
		}
		return false;
	}

}
